package com.yogee.topseller.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yogee.topseller.R;
import com.yogee.topseller.activity.ProductDetailsActivity;
import com.yogee.topseller.bean.MySelfInfo;
import com.yogee.topseller.fragment.product.model.bean.ProductBean;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.GlideUtils;

import java.util.List;

/**
 * 产品列表适配器
 *
 * @author hanyu
 *         created by 2017/6/28 17:18
 */

public class HProductListAdapter extends RecyclerView.Adapter<HProductListAdapter.ProductListHolder> {

    //产品数据
    List<ProductBean> productListBeen;
    //上下文
    Context mContext;
    //展示类型0.大图，1.列表
    int type = 0;
    //展示类型0.大图，1.列表
    private int TYPE_IMG = 0, TYPE_LIST = 1;


    public HProductListAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (type == 0) {
            return TYPE_IMG;
        }
        return TYPE_LIST;
    }

    /**
     * 根据类型刷新数据
     *
     * @param type 0.大图，1.列表
     * @param list
     */
    public void refreshData(int type, List<ProductBean> list) {
        this.type = type;
        this.productListBeen = list;
        notifyDataSetChanged();
    }


    @Override
    public ProductListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_IMG) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_is_big_img, parent, false);
            return new HProductListAdapter.ProductListHolder(view, viewType);
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_is_list, parent, false);
        return new HProductListAdapter.ProductListHolder(view, viewType);
    }

    @Override
    public void onBindViewHolder(ProductListHolder holder, final int position) {
        if (type == TYPE_IMG) {
            holder.big_img_name_tv.setText(productListBeen.get(position).getName());
            if ("秒结".equals(productListBeen.get(position).getLabel().toString())) {
                holder.img_mj_tv.setText(productListBeen.get(position).getLabel());
                holder.img_mj_tv.setVisibility(View.VISIBLE);
            } else {
                holder.img_mj_tv.setVisibility(View.GONE);
            }
            GlideUtils.defultImage(mContext, productListBeen.get(position).getImg(), holder.big_img_iv);
            holder.big_img_qisu_money_tv.setText(productListBeen.get(position).getStartAmount());
            holder.big_img_sy_money_tv.setText(productListBeen.get(position).getExpectReturn());
            holder.big_img_qx_day_tv.setText(productListBeen.get(position).getInvestmentTerm());
            holder.big_img_yong_bi_tv.setText(productListBeen.get(position).getCommission());
            holder.big_img_yong_pro_tv.setText(productListBeen.get(position).getPercent() + "%");
            if (productListBeen.get(position).getDepositDate() == null) {
                holder.big_img_date_tv.setVisibility(View.GONE);
            } else {
                holder.big_img_date_tv.setText(productListBeen.get(position).getDepositDate());
            }
            if (productListBeen.get(position).getCurrentInfo().equals("暂无信息")) {
                holder.big_img_details_tv.setVisibility(View.GONE);
            } else {
                holder.big_img_details_tv.setText(productListBeen.get(position).getCurrentInfo());
            }
            if (!TextUtils.isEmpty(productListBeen.get(position).getPercent())) {
                //33.33
                String[] splitPres = new String[2];
                splitPres = productListBeen.get(position).getPercent().split("\\.");
                if (splitPres.length>1){
                    holder.big_img_yong_pb.setProgress(Integer.parseInt(splitPres[0]));
                }else{
                    holder.big_img_yong_pb.setProgress(Integer.parseInt(productListBeen.get(position).getPercent()));
                }
            }
            holder.big_img_ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mContext.startActivity(new Intent(mContext, ProductDetailsActivity.class).putExtra("id", productListBeen.get(position).getId()).putExtra("isFast", "0"));
                }
            });
        } else {
            holder.item_list_name_tv.setText(productListBeen.get(position).getName());

            holder.item_list_details_tv.setText(productListBeen.get(position).getProType());
            holder.item_list_qt_money_tv.setText(productListBeen.get(position).getStartAmount());
            holder.item_list_sy_tv.setText(productListBeen.get(position).getExpectReturn().substring(0,productListBeen.get(position).getExpectReturn().length()-1));
            holder.item_list_month_tv.setText(productListBeen.get(position).getInvestmentTerm());
            if(AppUtil.isExamine(mContext)){
                holder.item_list_yong_bi_tv.setText(productListBeen.get(position).getCommission());
                holder.item_list_yong_bi_tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                if(MySelfInfo.getInstance().getIsPay().equals("0")){
                    if (!TextUtils.isEmpty(productListBeen.get(position).getPercent())) {
                        holder.item_list_yong_pro_tv.setText(productListBeen.get(position).getPercent() + "%");
                        holder.item_list_yong_bi_tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                        //33.33
                        String[] splitPres = new String[2];
                        splitPres = productListBeen.get(position).getPercent().split("\\.");
                        if (splitPres.length>1){
                            holder.item_list_yong_pb.setProgress(Integer.parseInt(splitPres[0]));
                        }else{
                            holder.item_list_yong_pb.setProgress(Integer.parseInt(productListBeen.get(position).getPercent()));
                        }
                    }
                }else{
                    holder.item_list_yong_pro_tv.setText("会员可见");
                    holder.item_list_yong_pro_tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
                    holder.item_list_yong_pb.setProgress(0);
                }

            }else{
                holder.item_list_yong_bi_tv.setText("请登录");
                holder.item_list_yong_bi_tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
                holder.item_list_yong_pro_tv.setText("请登录");
                holder.item_list_yong_pro_tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
                holder.item_list_yong_pb.setProgress(0);
            }


//            holder.item_list_yong_pb.setProgress(Integer.parseInt(productListBeen.get(position).getPercent()));
            holder.item_list_ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mContext.startActivity(new Intent(mContext, ProductDetailsActivity.class).putExtra("id", productListBeen.get(position).getId()).putExtra("isFast", "0"));
                }
            });

            // 预热和图片
            if(productListBeen.get(position).getImgstuats().equals("1")){
                holder.img_tu.setVisibility(View.VISIBLE);
            }else
                holder.img_tu.setVisibility(View.GONE);

            if(productListBeen.get(position).getStatus().equals("预热中"))
                holder.img_yu.setVisibility(View.VISIBLE);
            else
                holder.img_yu.setVisibility(View.GONE);
            if(productListBeen.get(position).getStatus().equals("售罄")){
                holder.img_sell_out.setVisibility(View.VISIBLE);
            }else
                holder.img_sell_out.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return null == productListBeen ? 0 : productListBeen.size();
    }

    class ProductListHolder extends RecyclerView.ViewHolder {

        //大图视图
        LinearLayout big_img_ll;
        TextView big_img_name_tv, img_mj_tv, big_img_qisu_money_tv, big_img_sy_money_tv, big_img_qx_day_tv, big_img_yong_bi_tv, big_img_yong_pro_tv, big_img_date_tv, big_img_details_tv;
        ImageView big_img_iv;
        ProgressBar big_img_yong_pb;
        //列表视图
        LinearLayout item_list_ll;
        TextView item_list_name_tv, item_list_mj_tv, item_list_details_tv, item_list_qt_money_tv, item_list_sy_tv, item_list_month_tv, item_list_yong_bi_tv, item_list_yong_pro_tv;
        ProgressBar item_list_yong_pb;
        ImageView img_tu,img_yu,img_sell_out;

        public ProductListHolder(View view, int viewType) {
            super(view);
            if (viewType == TYPE_IMG) {
                big_img_ll = (LinearLayout) view.findViewById(R.id.big_img_ll);
                big_img_name_tv = (TextView) view.findViewById(R.id.big_img_name_tv);
                img_mj_tv = (TextView) view.findViewById(R.id.img_mj_tv);
                big_img_iv = (ImageView) view.findViewById(R.id.big_img_iv);
                big_img_qisu_money_tv = (TextView) view.findViewById(R.id.big_img_qisu_money_tv);
                big_img_sy_money_tv = (TextView) view.findViewById(R.id.big_img_sy_money_tv);
                big_img_qx_day_tv = (TextView) view.findViewById(R.id.big_img_qx_day_tv);
                big_img_yong_bi_tv = (TextView) view.findViewById(R.id.big_img_yong_bi_tv);
                big_img_yong_pro_tv = (TextView) view.findViewById(R.id.big_img_yong_pro_tv);
                big_img_date_tv = (TextView) view.findViewById(R.id.big_img_date_tv);
                big_img_details_tv = (TextView) view.findViewById(R.id.big_img_details_tv);
                big_img_yong_pb = (ProgressBar) view.findViewById(R.id.big_img_yong_pb);
            } else {
                item_list_ll = (LinearLayout) view.findViewById(R.id.item_list_ll);
                item_list_name_tv = (TextView) view.findViewById(R.id.item_list_name_tv);
                item_list_mj_tv = (TextView) view.findViewById(R.id.item_list_mj_tv);
                item_list_details_tv = (TextView) view.findViewById(R.id.item_list_details_tv);
                item_list_qt_money_tv = (TextView) view.findViewById(R.id.item_list_qt_money_tv);
                item_list_sy_tv = (TextView) view.findViewById(R.id.item_list_sy_tv);
                item_list_month_tv = (TextView) view.findViewById(R.id.item_list_month_tv);
                item_list_yong_bi_tv = (TextView) view.findViewById(R.id.item_list_yong_bi_tv);
                item_list_yong_pro_tv = (TextView) view.findViewById(R.id.item_list_yong_pro_tv);
                item_list_yong_pb = (ProgressBar) view.findViewById(R.id.item_list_yong_pb);
                img_tu = (ImageView) view.findViewById(R.id.img_tu);
                img_yu = (ImageView) view.findViewById(R.id.img_yu);
                img_sell_out = (ImageView) view.findViewById(R.id.img_sell_out);
            }
        }
    }
}
